package serverClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import orders.Customer;

/*
 * Server thread work order to customer
 */
public class ServerThread extends Thread {
	private Socket socket;
	public static Map<String, Customer> customers = new HashMap<String, Customer>();

	public ServerThread(Socket socket) {
		this.socket = socket;
	}

	/*
	 * step 1: Server receive name customer from client
	 * step 2: server synchronized customer in list customers with name
	 * step 3: if step 2 can't run synchronized customers
	 * step 4: get customer from customers
	 * step 5: send customer to client
	 * step 6: receive customer from client
	 * step 7: save customer to list customers
	 * step 8: show all order of customer
	 */
	public void run() {
		try {
			// step 1
			System.out.println("Start thread server");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			String name = br.readLine();

			if (name != null) {
				Customer cus = null;
				try {
					// step 2
					synchronized (customers.get(name)) {
						// step 4
						cus = getCustomer(name);
						
						// step 5
						ObjectOutputStream oos = new ObjectOutputStream(
								socket.getOutputStream());
						oos.writeObject(cus);

						// step 6
						ObjectInputStream ios = new ObjectInputStream(
								socket.getInputStream());
						cus = (Customer) ios.readObject();
						
						// step 7
						saveCustomer(cus);

						// print all order of customer
						System.out.println("Print all order of "
								+ cus.getName());
						
						// step 8
						cus.getAllOrder();
						System.out.println("----------------------------------------------------------------------");
					}
				} catch (NullPointerException e) {
					// step 3
					synchronized (customers) {
						// step 4
						cus = getCustomer(name);
						
						// step 5
						ObjectOutputStream oos = new ObjectOutputStream(
								socket.getOutputStream());
						oos.writeObject(cus);

						// step 6
						ObjectInputStream ios = new ObjectInputStream(
								socket.getInputStream());
						cus = (Customer) ios.readObject();
						
						// step 7
						saveCustomer(cus);

						// step 8: print all order of customer
						System.out.println("Print all order of "
								+ cus.getName());
						
						cus.getAllOrder();
						System.out.println("----------------------------------------------------------------------");
					}
				}
			}

		} catch (IOException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * get customer from list customers with name
	 * if name not found create new customer
	 * receive customer
	 */
	private Customer getCustomer(String name) {
		Customer result = null;

		result = customers.get(name);

		if (result == null) {
			result = new Customer(name, Customer.ORTHER);
			customers.put(name, result);
		}

		return result;
	}

	/*
	 * Save customer to list customers
	 */
	private void saveCustomer(Customer customer) {
		customers.put(customer.getName(), customer);
	}
	
//	private void 

}
